package mobileann.mafamily.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

import mobileann.mafamily.act.main.FS;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Binder;
import android.os.Build;

import com.mofind.android.base.L;

public class StartDetectionUtils {
	private final static String property = "ro.miui.ui.version.name";

	public static boolean hasSetting(Context context) {
		boolean bret = true;
		if (isMIUI()) {
			int sdk_int = Build.VERSION.SDK_INT;
			// 判定自启动
			if (sdk_int >= 19) {
				try {
					PackageManager pm = context.getPackageManager();
					ApplicationInfo appInfo = pm.getApplicationInfo(context.getPackageName(), 0);
					bret = ((appInfo.flags & (1 << 26)) == (1 << 26)) ? false : true;
					// L.e("TAG===", "flag : "+appInfo.flags+ ", version : "
					// +getMIUIVersion());
				} catch (NameNotFoundException e) {
					L.e("maf", "error--", e);
				}
			} else {
				try {
					PackageManager pm = context.getPackageManager();
					ApplicationInfo appInfo = pm.getApplicationInfo(context.getPackageName(), 0);
					bret = ((appInfo.flags & (1 << 30)) == (1 << 30)) ? false : true;
					// L.e("TAG===", "flag : "+appInfo.flags+ ", version : "
					// +getMIUIVersion());
				} catch (NameNotFoundException e) {
					L.e("maf", "error--", e);
				}
			}
			// 判定悬浮窗
			// return (((context.getApplicationInfo().flags & 1<<25) == 1) ?
			// true : false);
		}
		return bret;
	}

	/**
	 * 判断MIUI的悬浮窗权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isMiuiFloaAllowed(Context context) {
		if (isMIUI()) {
			return checkOp(context);
		} else {
			return false;
		}
	}

	public static boolean checkOp(Context context) {
		try {
			if (AppOpsManager.MODE_ALLOWED == oM(context)) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	private static ApplicationInfo localApplicationInfo1 = null;

	private static int oM(Context context) {
		if (Build.VERSION.SDK_INT >= 19){
			try {
				Object localObject = FS.getInstance().getSystemService("appops");
				Class localClass = localObject.getClass();
				Class[] arrayOfClass = new Class[3];
				arrayOfClass[0] = Integer.TYPE;
				arrayOfClass[1] = Integer.TYPE;
				arrayOfClass[2] = String.class;
				Method localMethod = localClass.getMethod("checkOp", arrayOfClass);
				Object[] arrayOfObject = new Object[3];
				arrayOfObject[0] = Integer.valueOf(24);
				arrayOfObject[1] = Integer.valueOf(Binder.getCallingUid());
				arrayOfObject[2] = FS.getInstance().getPackageName();
				int j = ((Integer) localMethod.invoke(localObject, arrayOfObject)).intValue();
				if (j == 0)
					return 1;
				return 0;
			} catch (Throwable localThrowable2) {
				return -1;
			}
		}else{
			ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);  
			ComponentName cn = am.getRunningTasks(1).get(0).topActivity;  
			if(!"com.mobileann.mafamily".equalsIgnoreCase(cn.getPackageName())){
				try {
					ApplicationInfo localApplicationInfo2 = FS
							.getInstance().getPackageManager()
							.getApplicationInfo(FS.getInstance().getPackageName(),0);
					localApplicationInfo1 = localApplicationInfo2;
					if (localApplicationInfo1 != null) {
						if ((0x8000000 & localApplicationInfo1.flags) == 0)
							return 0;
					}
				} catch (Throwable localThrowable1) {
					while (true)
						localApplicationInfo1 = null;
				}
			}
			return -1;
		}
	}

	// =======
	/*
	 * public static boolean hasSetting(Context context){ final PackageManager
	 * pm = context.getPackageManager(); final Intent intent = new
	 * Intent(Intent.ACTION_BOOT_COMPLETED, null); List<ResolveInfo> list =
	 * pm.queryBroadcastReceivers(intent,pm.GET_DISABLED_COMPONENTS);
	 * for(ResolveInfo info : list){ ApplicationInfo appInfo =
	 * info.activityInfo.applicationInfo; if ((appInfo.flags &
	 * ApplicationInfo.FLAG_SYSTEM) !=0 ) { continue; }
	 * 
	 * ComponentName compName = new ComponentName(appInfo.packageName,
	 * info.activityInfo.name); if (pm.getComponentEnabledSetting(compName) !=
	 * PackageManager.COMPONENT_ENABLED_STATE_DISABLED) { L.i("TAG===",
	 * info.loadLabel(pm).toString()+":启用"); } else { L.i("TAG===",
	 * info.loadLabel(pm).toString()+":禁用"); } } L.i("TAG===",
	 * "============================================="); return false; } >>>>>>>
	 * .r4231 // int size = list.size(); // int allStatus[] = new int[size]; //
	 * for(int i = 0;i < size;i++){ // ResolveInfo info= list.get(i); //
	 * ComponentName mComponentName = new
	 * ComponentName(info.activityInfo.packageName, info.activityInfo.name); //
	 * String name = info.loadLabel(packageManager).toString(); //
	 * packageManager.getComponentEnabledSetting(mComponentName); ////
	 * allStatus[i] = status; // L.d("TAG===", name+":"+info.priority); ////
	 * if(name.equals("跟屁虫")){ //// index = i; //// } // } //
	 * if(allStatus[index] == 1){ // return false; // }else{ // for(int sta :
	 * allStatus){ // if(sta == 1){ // return true; // } // } // }
	 */
	// ==============//

	/**
	 * 根据版本号判断是否是MIUI
	 * @return
	 */
	public static boolean isMIUI() {
//		String brand = Build.BRAND.toString();
		if ("Xiaomi".equalsIgnoreCase(Build.BRAND.toString())) {
			return true;
		}
		return false;
	}

	/**
	 * Letv 判断是否是乐视手机
	 * @return
	 */
	public static boolean isLETV(){
//		String brand = Build.BRAND.toString();
		if("Letv".equalsIgnoreCase(Build.BRAND.toString())) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断是不是华为手机
	 * @return
	 */
	public static boolean isHUAWEI(){
		if("Huawei".equalsIgnoreCase(Build.BRAND.toString())) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断是不是魅族手机
	 * @return
	 */
	public static boolean isMEIZU(){
		if("Meizu".equalsIgnoreCase(Build.BRAND.toString())) {
			return true;
		}
		return false;
	}

	public static boolean isSAMSUNG(){
		if("samsung".equalsIgnoreCase(Build.BRAND.toString())) {
			return true;
		}
		return false;
	}
	
	public static boolean isVIVO(){
		if ("vivo".equalsIgnoreCase(Build.BRAND.toString())) {
			return true;
		}
		return false;
	}
	
	/**
	 * 获取MIUI版本号
	 * 
	 */
	public static String getSystemProperty(String propName) {
		String line;
		BufferedReader input = null;
		try {
			Process p = Runtime.getRuntime().exec("getprop " + propName);
			input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
			line = input.readLine();
			input.close();
		} catch (IOException ex) {
			L.e("TAG", "Unable to read sysprop " + propName, ex);
			return null;
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					L.e("TAG", "Exception while closing InputStream", e);
				}
			}
		}
		return line;
	}

	public static String getMIUIVersion() {
		return getSystemProperty(property);
	}

	public static String getPkgName(Context context) {
		String packageName = "";
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
			packageName = info.packageName;
		} catch (NameNotFoundException e) {
			L.e("maf", "error--", e);
		}
		return packageName;
	}
}
